<?php
namespace app\app\model;
use think\Db;
use think\Model;

class HouseModel extends Model{

    protected $table = "a_house";

    /**
     * 根据id获取题目
     * @param $id
     */
    public function getOne($id){
        return $this->where('id','eq',$id)->find();
    }

    /**
     * 条件查询题目
     */
    public function getHouse($id){
        $data = $this->where('id','not in',$id)->select();
        //多项随机取一项
        $count = count($data);
		$count = $count-1;
        $house_id = rand(0,$count);
        $block = $data[$house_id];
        return $block;
    }

    /**
     * 统计总数
     * @return int|string
     */
    public function getNum(){
        return $this->where('status','eq',1)->count();
    }

    /**
     * 获取对战题目
     * @param $id
     */
    public function getFight(){
        $id = array();
        $data = array();
        for($i=0;$i<5;$i++){
            $house = $this->getHouse($id);
            $id[]  = $house['id'];
            $data[] = $house;
        }
        foreach($data as $m => $n){
            //处理数据
            $result = json_decode($n['result'],true);
            $arr  = array(array('id'=>'A','name'=>$result['A']),array('id'=>'B','name'=>$result['B']),array('id'=>'C','name'=>$result['C']),array('id'=>'D','name'=>$result['D']));
            foreach($arr as $k => $v){
                if($v['id'] == $n['answer']){
                    $arr[$k]['is_true'] = 1;
                }else{
                    $arr[$k]['is_true'] = 0;
                }
            }
            $data[$m]['answer'] = $arr;
            unset($data[$m]['result']);
            unset($data[$m]['status']);
        }
        return $data;
    }
}